package com.regulareport.web.controller.report;

import com.regulareport.common.annotation.Log;
import com.regulareport.common.core.controller.BaseController;
import com.regulareport.common.core.domain.AjaxResult;
import com.regulareport.common.core.page.TableDataInfo;
import com.regulareport.common.enums.BusinessType;
import com.regulareport.form.domain.*;
import com.regulareport.form.service.impl.ReportPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/report/plan")
@Api(tags = "计划管理")
public class ReportPlanController extends BaseController {
    private final ReportPlanService reportPlanService;

    public ReportPlanController(ReportPlanService reportPlanService) {
        this.reportPlanService = reportPlanService;
    }

    /**
     * 新增下发计划
     */
    @PreAuthorize("@ss.hasPermi('report:plan:add')")
    @ApiOperation("新增下发计划")
    @Log(title = "新增下发计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AddReportPlanReq reportPlanReq) {
        try {
            reportPlanService.addReportPlan(reportPlanReq);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改下发计划
     */
    @PreAuthorize("@ss.hasPermi('report:plan:update')")
    @Log(title = "修改下发计划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation("修改下发计划")
    public AjaxResult update(@RequestBody EditReportPlanReq editReportPlanReq) {
        try {
            reportPlanService.editReportPlan(editReportPlanReq);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取下发计划详情
     */
    @PreAuthorize("@ss.hasPermi('report:plan:detail')")
    @PostMapping("/detail")
    @ApiOperation("获取下发计划详情")
    public AjaxResult detail(@RequestBody Detail detail) {
        try {
            ReportPlanAllRep reportDept = reportPlanService.detail(detail);
            return AjaxResult.success(reportDept);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取下发计划列表
     */
    @PreAuthorize("@ss.hasPermi('report:plan:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ReportPlanListReq req) {
        startPage();
        List<ReportPlanAllRep> reportDept = reportPlanService.list(req);
        return getDataTable(reportDept);
    }

    /**
     * 删除下发计划
     */
    @PreAuthorize("@ss.hasPermi('report:plan:delete')")
    @Log(title = "删除下发计划", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult del(@RequestBody Detail detail) {
        try {
            reportPlanService.delete(detail);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('report:plan:enable')")
    @Log(title = "计划启停", businessType = BusinessType.UPDATE)
    @PostMapping("/enable")
    public AjaxResult enable(@RequestBody Detail detail) {
        try {
            reportPlanService.enable(detail);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 计划下发
     */
    @PreAuthorize("@ss.hasPermi('report:plan:delivery')")
    @Log(title = "计划下发", businessType = BusinessType.UPDATE)
    @PostMapping("/delivery")
    public AjaxResult delivery(@RequestBody DeliveryReq detail) {
        try {
            reportPlanService.delivery(detail);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
